import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SpringLayout;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JButton;



public class ConfigurationPanel extends JPanel {
	private JRadioButton farmerRDB, bankerRDB, carpenterRDB;
	private Player player;

	private JTextField playerNameTF;
	private final ButtonGroup radioButtonGroup = new ButtonGroup();
	private final ButtonGroup radioButtonGroup2 = new ButtonGroup();
	private final ButtonGroup radioButtonGroup3 = new ButtonGroup();
	private JTextField partyMemberOneTF;
	private JTextField partyMemberTwoTF;
	private JTextField partyMemberThreeTF;
	private JTextField partyMemberFourTF;
	private JButton startButton;
	private JRadioButton rdbtnLow;
	private JRadioButton rdbtnMedium;
	private JRadioButton rdbtnHigh;
	private JRadioButton rdbtnLow_1;
	private JRadioButton rdbtnHigh_1;
	private JRadioButton rdbtnMedium_1;
	
	/**
	 * Create the panel.
	 */
	public ConfigurationPanel() {
		setPreferredSize(new Dimension(500,600));
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);
		
		JLabel lblPlayerName = new JLabel("Player Name");
		springLayout.putConstraint(SpringLayout.NORTH, lblPlayerName, 34, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, lblPlayerName, 10, SpringLayout.WEST, this);
		add(lblPlayerName);
		
		this.playerNameTF = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, playerNameTF, 0, SpringLayout.NORTH, lblPlayerName);
		springLayout.putConstraint(SpringLayout.WEST, playerNameTF, 6, SpringLayout.EAST, lblPlayerName);
		add(playerNameTF);
		playerNameTF.setColumns(10);
		
		JLabel lblPlayerProfession = new JLabel("Player Profession");
		springLayout.putConstraint(SpringLayout.NORTH, lblPlayerProfession, 0, SpringLayout.NORTH, lblPlayerName);
		add(lblPlayerProfession);
		
		this.farmerRDB = new JRadioButton("Farmer");
		farmerRDB.setSelected(true);
		springLayout.putConstraint(SpringLayout.EAST, lblPlayerProfession, -43, SpringLayout.WEST, farmerRDB);
		springLayout.putConstraint(SpringLayout.NORTH, farmerRDB, -4, SpringLayout.NORTH, lblPlayerName);
		radioButtonGroup.add(farmerRDB);
		add(farmerRDB);
		
		this.bankerRDB = new JRadioButton("Banker");
		springLayout.putConstraint(SpringLayout.NORTH, bankerRDB, 6, SpringLayout.SOUTH, farmerRDB);
		springLayout.putConstraint(SpringLayout.EAST, bankerRDB, -58, SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.EAST, farmerRDB, 0, SpringLayout.EAST, bankerRDB);
		radioButtonGroup.add(bankerRDB);
		add(bankerRDB);
		
		this.carpenterRDB = new JRadioButton("Carpenter");
		springLayout.putConstraint(SpringLayout.NORTH, carpenterRDB, 10, SpringLayout.SOUTH, bankerRDB);
		springLayout.putConstraint(SpringLayout.EAST, carpenterRDB, -42, SpringLayout.EAST, this);
		radioButtonGroup.add(carpenterRDB);
		add(carpenterRDB);
		
		JLabel lblPartyMember = new JLabel("Party Member 1");
		springLayout.putConstraint(SpringLayout.NORTH, lblPartyMember, 116, SpringLayout.SOUTH, lblPlayerName);
		springLayout.putConstraint(SpringLayout.WEST, lblPartyMember, 10, SpringLayout.WEST, this);
		add(lblPartyMember);
		
		partyMemberOneTF = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, partyMemberOneTF, 0, SpringLayout.NORTH, lblPartyMember);
		springLayout.putConstraint(SpringLayout.WEST, partyMemberOneTF, 6, SpringLayout.EAST, lblPartyMember);
		add(partyMemberOneTF);
		partyMemberOneTF.setColumns(10);
		
		JLabel lblPartyMember_1 = new JLabel("Party Member 2");
		springLayout.putConstraint(SpringLayout.NORTH, lblPartyMember_1, 28, SpringLayout.SOUTH, lblPartyMember);
		springLayout.putConstraint(SpringLayout.WEST, lblPartyMember_1, 10, SpringLayout.WEST, this);
		add(lblPartyMember_1);
		
		partyMemberTwoTF = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, partyMemberTwoTF, 0, SpringLayout.NORTH, lblPartyMember_1);
		springLayout.putConstraint(SpringLayout.EAST, partyMemberTwoTF, 0, SpringLayout.EAST, partyMemberOneTF);
		add(partyMemberTwoTF);
		partyMemberTwoTF.setColumns(10);
		
		JLabel lblPartyMember_2 = new JLabel("Party Member 3");
		springLayout.putConstraint(SpringLayout.NORTH, lblPartyMember_2, 0, SpringLayout.NORTH, lblPartyMember);
		add(lblPartyMember_2);
		
		partyMemberThreeTF = new JTextField();
		springLayout.putConstraint(SpringLayout.EAST, lblPartyMember_2, -28, SpringLayout.WEST, partyMemberThreeTF);
		springLayout.putConstraint(SpringLayout.NORTH, partyMemberThreeTF, -3, SpringLayout.NORTH, lblPartyMember);
		springLayout.putConstraint(SpringLayout.EAST, partyMemberThreeTF, -25, SpringLayout.EAST, this);
		add(partyMemberThreeTF);
		partyMemberThreeTF.setColumns(10);
		
		JLabel lblPartyMember_3 = new JLabel("Party Member 4");
		springLayout.putConstraint(SpringLayout.NORTH, lblPartyMember_3, 0, SpringLayout.NORTH, lblPartyMember_1);
		springLayout.putConstraint(SpringLayout.EAST, lblPartyMember_3, 0, SpringLayout.EAST, lblPartyMember_2);
		add(lblPartyMember_3);
		
		partyMemberFourTF = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, partyMemberFourTF, -3, SpringLayout.NORTH, lblPartyMember_1);
		springLayout.putConstraint(SpringLayout.EAST, partyMemberFourTF, 0, SpringLayout.EAST, partyMemberThreeTF);
		add(partyMemberFourTF);
		partyMemberFourTF.setColumns(10);
		
		this.startButton = new JButton("Start");
		springLayout.putConstraint(SpringLayout.WEST, startButton, 209, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, startButton, -79, SpringLayout.SOUTH, this);
		add(startButton);
		
		JLabel lblRations = new JLabel("Rations");
		springLayout.putConstraint(SpringLayout.NORTH, lblRations, 76, SpringLayout.SOUTH, lblPartyMember_1);
		springLayout.putConstraint(SpringLayout.WEST, lblRations, 0, SpringLayout.WEST, lblPlayerName);
		add(lblRations);
		
		JLabel lblPace = new JLabel("Pace");
		springLayout.putConstraint(SpringLayout.NORTH, lblPace, 0, SpringLayout.NORTH, lblRations);
		springLayout.putConstraint(SpringLayout.WEST, lblPace, 0, SpringLayout.WEST, lblPartyMember_2);
		add(lblPace);
		
		rdbtnLow = new JRadioButton("Low");
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnLow, 0, SpringLayout.NORTH, lblRations);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnLow, 0, SpringLayout.WEST, partyMemberOneTF);
		add(rdbtnLow);
		radioButtonGroup2.add(rdbtnLow);
		rdbtnLow.setSelected(true);

		
		rdbtnMedium = new JRadioButton("Medium");
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnMedium, 6, SpringLayout.SOUTH, rdbtnLow);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnMedium, 0, SpringLayout.WEST, partyMemberOneTF);
		add(rdbtnMedium);
		radioButtonGroup2.add(rdbtnMedium);

		rdbtnHigh = new JRadioButton("High");
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnHigh, 6, SpringLayout.SOUTH, rdbtnMedium);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnHigh, 0, SpringLayout.WEST, partyMemberOneTF);
		add(rdbtnHigh);
		radioButtonGroup2.add(rdbtnHigh);

		rdbtnLow_1 = new JRadioButton("Low");
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnLow_1, -4, SpringLayout.NORTH, lblRations);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnLow_1, 20, SpringLayout.EAST, lblPace);
		add(rdbtnLow_1);
		radioButtonGroup3.add(rdbtnLow_1);
		rdbtnLow_1.setSelected(true);

		rdbtnMedium_1 = new JRadioButton("Medium");
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnMedium_1, 0, SpringLayout.NORTH, rdbtnMedium);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnMedium_1, 0, SpringLayout.WEST, rdbtnLow_1);
		add(rdbtnMedium_1);
		radioButtonGroup3.add(rdbtnMedium_1);

		
		rdbtnHigh_1 = new JRadioButton("High");
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnHigh_1, 0, SpringLayout.NORTH, rdbtnHigh);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnHigh_1, 0, SpringLayout.WEST, rdbtnLow_1);
		add(rdbtnHigh_1);
		radioButtonGroup3.add(rdbtnHigh_1);


		listener event = new listener();
		startButton.addActionListener(event);
	}
	
	
	private class listener implements ActionListener{
		
		
		public void actionPerformed(ActionEvent event){
			if(event.getSource() == startButton)
			{
					
				if((playerNameTF.getText().length()>0)&&(partyMemberOneTF.getText().length()>0)&&(partyMemberTwoTF.getText().length()>0)&&(partyMemberThreeTF.getText().length()>0)&&(partyMemberFourTF.getText().length()>0))
				{
					if(carpenterRDB.isSelected())
					{
						player = new Carpenter(playerNameTF.getText());
					}
					if(bankerRDB.isSelected())
					{
						player = new Banker(playerNameTF.getText());
					}
					if(farmerRDB.isSelected())
					{
						player = new Farmer(playerNameTF.getText());
	
					}
					
					PartyMember pm1 = new PartyMember(partyMemberOneTF.getText());
					PartyMember pm2 = new PartyMember(partyMemberTwoTF.getText());
					PartyMember pm3 = new PartyMember(partyMemberThreeTF.getText());
					PartyMember pm4 = new PartyMember(partyMemberFourTF.getText());
					OregonTrailApp.party = new Party(player, pm1,pm2,pm3,pm4);
					
					System.out.println("Party is Created");
						
					
					if(rdbtnLow.isSelected())
					{
						OregonTrailApp.settingManager.setRation(eRation.low);
					}
					else if(rdbtnMedium.isSelected())
					{
						OregonTrailApp.settingManager.setRation(eRation.medium);
					}
					else if(rdbtnHigh.isSelected())
					{
						OregonTrailApp.settingManager.setRation(eRation.high);
					}
					
					if(rdbtnLow_1.isSelected())
					{
						OregonTrailApp.settingManager.setPace(ePace.low);
					}
					else if(rdbtnMedium_1.isSelected())
					{
						OregonTrailApp.settingManager.setPace(ePace.medium);
					}
					else if(rdbtnHigh_1.isSelected())
					{
						OregonTrailApp.settingManager.setPace(ePace.high);
					}
					
					OregonTrailApp.testing();
				}
			}
		}
		
	}
}
